package com.bear.newsset.util;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.appcompat.app.AlertDialog;

public class DialogUtil {
    private static DialogUtil mDialogUtil = null;
    private Context mContext;
    public interface DialogOnClickListener {
        void setPositiveButton(DialogInterface dialogInterface, int i);
        void setNegativeButton(DialogInterface dialogInterface, int i);
    }
    public static class DialogOnClickAdapter implements DialogOnClickListener {
        @Override
        public void setPositiveButton(DialogInterface dialogInterface, int i) { }
        @Override
        public void setNegativeButton(DialogInterface dialogInterface, int i) { }
    }

    private DialogUtil(){}
    public static DialogUtil getInstance() {
        if( mDialogUtil == null ) { mDialogUtil = new DialogUtil(); }
        return mDialogUtil;
    }

    public DialogUtil setContext(Context mContext) {
        this.mContext = mContext;
        return mDialogUtil;
    }

    /**
     * 显示一个Dialog
     * @param config                        dialog的配置
     * @param mDialogOnClickAdapter         点击事件适配器
     * @return
     */
    public DialogEntity createDialog(DialogConfig config, final DialogOnClickAdapter mDialogOnClickAdapter) {
        AlertDialog.Builder b = new AlertDialog.Builder(mContext);
        CharSequence title = config.getTitle();
        CharSequence contentText = config.getContentText();
        View contentView = config.getContentView();
        CharSequence pText = config.getPositiveButtonText();
        CharSequence nText = config.getNegativeButtonText();
        int pId = config.getPositiveButtonId();
        int nId = config.getNegativeButtonId();
        if( title != null ) { b.setTitle(title); }
        if( contentView != null ) {
            b.setView(contentView);
        }else if( contentText != null && !contentText.toString().equals("") ) {
            b.setMessage(contentText);
        }
        //积极的按钮
        if( mDialogOnClickAdapter != null ) {
            if( pId > 0 ) {
                b.setPositiveButton(pId, mDialogOnClickAdapter::setPositiveButton);
            }else if( pText != null && !pText.equals("") ) {
                b.setPositiveButton(pText, mDialogOnClickAdapter::setPositiveButton);
            }
        }
        //消极的按钮
        if( mDialogOnClickAdapter != null ) {
            if( nId > 0 ) {
                b.setPositiveButton(nId, mDialogOnClickAdapter::setPositiveButton);
            }else if( nText != null && !nText.equals("") ) {
                b.setNegativeButton("更新", mDialogOnClickAdapter::setNegativeButton);
            }
        }
        return new DialogEntity().setAlertDialog(b.create()).setView(contentView);
    }

    /**
     * dialog的配置
     */
    static class DialogConfig {
        private CharSequence title;
        private View contentView;
        private CharSequence contentText;
        private CharSequence positiveButtonText;
        private CharSequence negativeButtonText;
        private int positiveButtonId;
        private int negativeButtonId;

        DialogConfig() {}

        DialogConfig(CharSequence title, View contentView, CharSequence contentText,
                     CharSequence positiveButtonText, CharSequence negativeButtonText,
                            int positiveButtonId, int negativeButtonId) {
            this.title = title;
            this.contentView = contentView;
            this.contentText = contentText;
            this.positiveButtonText = positiveButtonText;
            this.negativeButtonText = negativeButtonText;
            this.positiveButtonId = positiveButtonId;
            this.negativeButtonId = negativeButtonId;
        }

        public CharSequence getTitle() {
            return title;
        }

        public View getContentView() {
            return contentView;
        }

        public CharSequence getContentText() {
            return contentText;
        }

        public CharSequence getPositiveButtonText() {
            return positiveButtonText;
        }

        public CharSequence getNegativeButtonText() {
            return negativeButtonText;
        }

        public int getPositiveButtonId() {
            return positiveButtonId;
        }

        public int getNegativeButtonId() {
            return negativeButtonId;
        }

        public DialogConfig setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public DialogConfig setContentView(View contentView) {
            this.contentView = contentView;
            return this;
        }

        public DialogConfig setContentText(CharSequence contentText) {
            this.contentText = contentText;
            return this;
        }

        public DialogConfig setPositiveButtonText(CharSequence positiveButtonText) {
            this.positiveButtonText = positiveButtonText;
            return this;
        }

        public DialogConfig setNegativeButtonText(CharSequence negativeButtonText) {
            this.negativeButtonText = negativeButtonText;
            return this;
        }

        public DialogConfig setPositiveButtonId(int positiveButtonId) {
            this.positiveButtonId = positiveButtonId;
            return this;
        }

        public DialogConfig setNegativeButtonId(int negativeButtonId) {
            this.negativeButtonId = negativeButtonId;
            return this;
        }

        static class Builder {
            private CharSequence title;
            private View contentView;
            private CharSequence contentText;
            private CharSequence positiveButtonText;
            private CharSequence negativeButtonText;
            private int positiveButtonId;
            private int negativeButtonId;
            Builder setTitle(CharSequence title) {
                this.title = title;
                return this;
            }
            Builder setContentView(View contentView) {
                this.contentView = contentView;
                return this;
            }
            Builder setContentText(CharSequence contentText) {
                this.contentText = contentText;
                return this;
            }
            Builder setPositiveButtonText(CharSequence positiveButtonText) {
                this.positiveButtonText = positiveButtonText;
                return this;
            }
            Builder setNegativeButtonText(CharSequence negativeButtonText) {
                this.negativeButtonText = negativeButtonText;
                return this;
            }
            Builder setPositiveButtonId(int positiveButtonId) {
                this.positiveButtonId = positiveButtonId;
                return this;
            }
            Builder setNegativeButtonId(int negativeButtonId) {
                this.negativeButtonId = negativeButtonId;
                return this;
            }
            DialogConfig build() {
                return new DialogConfig(title, contentView, contentText,
                                        positiveButtonText, negativeButtonText,
                                        positiveButtonId, negativeButtonId);
            }
        }
    }

    class DialogEntity {
        private View view;
        private AlertDialog alertDialog;

        public View getView() {
            return view;
        }

        public DialogEntity setView(View view) {
            this.view = view;
            return this;
        }

        public AlertDialog getAlertDialog() {
            return alertDialog;
        }

        public DialogEntity setAlertDialog(AlertDialog alertDialog) {
            this.alertDialog = alertDialog;
            return this;
        }
    }
}
